using System;
using System.Collections.Generic;
using UnityEngine;

public class CardProfile : MonoBehaviour
{
	[Serializable]
	public class SkillGain
	{
		public PlayerStatsManager.SkillType skillType;

		public float percentage;
	}

	[Serializable]
	public class SkillGainDescription
	{
		public string type;

		public List<SkillGain> gains = new List<SkillGain>();

		public SkillGain GetSkillGainValue(int skillTypeIndex)
		{
			foreach (SkillGain gain in gains)
			{
				if (gain.skillType == (PlayerStatsManager.SkillType)skillTypeIndex)
				{
					return gain;
				}
			}
			return null;
		}
	}

	public GameObject skillDisplayButtonPrefab;

	public Vector3 localOffsetButtons = Vector3.zero;

	protected List<SkillLevelDisplay> skillButtons = new List<SkillLevelDisplay>();

	public float skillGain;

	protected string type;

	public List<SkillGainDescription> gains = new List<SkillGainDescription>();

	private PlayerStatsManager character;

	private void OnEnable()
	{
		PlayerStatsManager statsManager = GGPlayerSettings.instance.statsManager;
		Init(statsManager);
	}

	private void Init(PlayerStatsManager character)
	{
		this.character = character;
		int num = 0;
		PlayerStats playerStats = character.playerStats;
		UITools.SetActive(skillDisplayButtonPrefab, false);
		Vector3 localPosition = skillDisplayButtonPrefab.transform.localPosition;
		PlayerStatsManager.SkillType[] displaySkills = playerStats.displaySkills;
		foreach (PlayerStatsManager.SkillType skillType in displaySkills)
		{
			if (num >= skillButtons.Count)
			{
				Transform parent = skillDisplayButtonPrefab.transform.parent;
				SkillLevelDisplay component = NGUITools.AddChild(parent.gameObject, skillDisplayButtonPrefab.gameObject).GetComponent<SkillLevelDisplay>();
				skillButtons.Add(component);
			}
			SkillLevelDisplay skillLevelDisplay = skillButtons[num];
			skillLevelDisplay.transform.localPosition = localPosition + localOffsetButtons * num;
			skillLevelDisplay.gameObject.name = GGFormat.IndexName(num);
			skillLevelDisplay.gameObject.SetActive(true);
			skillLevelDisplay.Init(character, skillType);
			num++;
		}
		for (int j = num; j < skillButtons.Count; j++)
		{
			SkillLevelDisplay beh = skillButtons[j];
			UITools.SetActive(beh, false);
		}
	}

	private SkillGainDescription GetGain(string type)
	{
		foreach (SkillGainDescription gain in gains)
		{
			if (gain.type == type)
			{
				return gain;
			}
		}
		return null;
	}

	private void Update()
	{
		SkillGainDescription gain = GetGain(type);
		if (gain == null)
		{
			return;
		}
		foreach (SkillLevelDisplay skillButton in skillButtons)
		{
			SkillGain skillGainValue = gain.GetSkillGainValue(skillButton.skillTypeIndex);
			if (skillGainValue != null)
			{
				skillButton.SetGainFill(skillGainValue.percentage * skillGain);
			}
		}
	}

	public void SetTypeAllRound()
	{
		type = "allround";
	}

	public void SetTypeAttack()
	{
		type = "attack";
	}

	public void SetTypeDefense()
	{
		type = "defense";
	}

	public void SetTypeChopper()
	{
		type = "chopper";
	}

	public void ResetSkills()
	{
		character.playerStats.ResetAllStats();
		Init(character);
	}

	public void AddSkills()
	{
		SkillGainDescription gain = GetGain(type);
		if (gain == null)
		{
			return;
		}
		foreach (SkillLevelDisplay skillButton in skillButtons)
		{
			SkillGain skillGainValue = gain.GetSkillGainValue(skillButton.skillTypeIndex);
			if (skillGainValue != null)
			{
				float skillChange = skillGainValue.percentage * skillGain;
				character.playerStats.UpdateSkill(skillChange, skillButton.skillTypeIndex);
			}
		}
		type = null;
		Init(character);
	}
}
